package top.springmvc;


import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import top.mybatis.entitys.User;
import top.mybatis.mapper.UserMapper;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.util.List;

/**
 * @author 刘杨
 */
@Controller
public class HelloController extends HttpServlet {
  
  @Resource
  UserMapper userMapper;
  
  /**  /userLoad => /userLoad.jsp  userLoad.* */
  @RequestMapping("/userLoad")
  @ResponseBody
  public void userLoad() {
    List<User> users = userMapper.selectAll();
    System.out.println(users.toString());
    // 1. / userLoad .jsp
    // 2. /userLoad.*
  }
  
  
  @RequestMapping("/ly")
  @ResponseBody
  public String ly(@RequestParam("a7") CommonsMultipartFile a7, HttpServletRequest req, HttpServletResponse resp, String a1, String a2) throws IOException {
    
    InputStream is = a7.getInputStream();
    OutputStream os = new FileOutputStream("C:/xxx.jpg");
    
    // InputStreamReader  字符  字符串流
    // OutputStreamWriter 字符
    
    byte[] bytes = new byte[1024 * 10];
    int length = -1;
    while ((length = is.read(bytes)) != -1) {
      os.write(bytes, 0, length);
    }
    os.close();
    
    //过滤器 写下面的
    try {
      
      throw new FileNotFoundException();
      
    } catch (FileNotFoundException ex) {
      resp.setStatus(404);
    } catch (NullPointerException ex) {
      resp.setStatus(500);
    }
    
    
    System.out.println(a1);
    return "直接返回文本内容";
  }
  
  @RequestMapping("/img")
  public void showImg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  
    /*
    //所有 二进制类型文件都能输出
    InputStream is=new FileInputStream("F:\\Gensir\\Documents\\Visual Studio 2015\\Projects\\MemberManage - 副本\\Angular2 web app1\\Assets\\WapUI\\img\\V3\\Index\\Product\\index-zjz-v3.jpg");
  
    resp.setContentType("application/octet-stream");//控制输出 内容的类型
    resp.setHeader("Content-Disposition","attachment;filename="+"xxxxx.jpg");//设置文件名称
    ServletOutputStream os = resp.getOutputStream();
    
    
    byte[] bytes=new byte[1024*10];
    int length=-1;
    while ((length=is.read(bytes))!=-1){
      os.write(bytes,0,length);
    }
    is.close();*/
    
    //new FileOutputStream("变量");
    
    // 1。文件上传/下载
    // 2。数据提交/返回
    //只能图片输出
    // webSocket web聊天室
    // Java Socket 聊天
    
    
    resp.setContentType("image/jpeg");
    //控制输出 内容的类型
    BufferedImage img = ImageIO.read(new File("F:\\Gensir\\Documents\\Visual Studio 2015\\Projects\\MemberManage - 副本\\Angular2 web app1\\Assets\\WapUI\\img\\V3\\Index\\Product\\index-zjz-v3.jpg"));
    ImageIO.write(img, "jpeg", resp.getOutputStream());
    
  }
  
  // bb(req,,"")
  
}
